package org.lem.auth

import org.apache.shiro.crypto.hash.Sha1Hash


class LoginCreateCommand
{
    String username
    String password
    String passwordCompare
    Set roles = []

    static constraints = {
        username nullable: false, blank: false, validator: { val, obj ->
            if (ShiroUser.findByUsername(val) != null) {
                return false
            }
        }
        password nullable: false, blank: false
        roles minSize: 1
        passwordCompare validator: { val, obj ->
            if(val != obj.password) {
                return false
            }
        }
    }

    ShiroUser createLogin()
    {
        def userRoles = roles.collect { role -> ShiroRole.get(role) }
        def user = new ShiroUser(username: username, passwordHash: new Sha1Hash(password).toHex(), roles: userRoles)
        return user
    }
}
